package nl.stroep.classbuilder.project 
{
	import adobe.utils.ProductManager;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.SharedObject;
	import flash.xml.XMLNode;
	import nl.stroep.classbuilder.cb.CbClass;
	import nl.stroep.classbuilder.cb.CbMethod;
	import nl.stroep.classbuilder.cb.CbParameter;
	import nl.stroep.classbuilder.cb.CbVariable;
	
	public class ProjectManager extends EventDispatcher
	{
		private const extension:String = ".xml";
		
		private static var _instance:ProjectManager;
		
		public var projects:Vector.<Project> = new Vector.<Project>();
		public var current:Project;
		private var fileRef:FileReference;
		
		public function ProjectManager(enforcer:SingletonEnforcer) 
		{
			if (enforcer == null)
			{
				throw new Error("Singleton: Do not instantiate ProjectManager with the constructor.");
			}
		}
		
		public static function getInstance():ProjectManager 
		{
			if(!_instance)
			{
				_instance = new ProjectManager(new SingletonEnforcer());
			}
			
			return _instance;
		}
		
		public function save():void
		{	
			var fileRef:FileReference = new FileReference();
			fileRef.save(current.toXML(), current.name + extension);
		}
		
		public function load():void
		{	
			readCookies();
			
			fileRef = new FileReference();
			
			fileRef.addEventListener(Event.SELECT, onFileSelected);
			fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
			fileRef.addEventListener(ProgressEvent.PROGRESS, onFileProgress);
			fileRef.addEventListener(IOErrorEvent.IO_ERROR, onFileError);
			
			fileRef.browse();
		}
		
		private function onFileProgress(e:ProgressEvent):void 
		{
			dispatchEvent(e);
		}
		
		private function onFileError(e:IOErrorEvent):void 
		{
			trace("error while loading project")
		}
		
		private function onFileLoaded(e:Event):void 
		{			
			var xml:XML = new XML(fileRef.data);
			addFromXML(xml);
			
			addToCookie(fileRef);
			
			dispatchEvent(new Event(Event.COMPLETE));
			
			fileRef.removeEventListener(Event.SELECT, onFileSelected);
			fileRef.removeEventListener(Event.COMPLETE, onFileLoaded);
			fileRef.removeEventListener(IOErrorEvent.IO_ERROR, onFileError);
			fileRef = null;
		}
		
		public function addFromXML(xml:XML):void
		{
			var project:Project = Project.parseData(xml);
			
			this.add(project);
		}
		
		private function onFileSelected(e:Event):void 
		{
			fileRef.load();
		}
		
		public function add(project:Project):void
		{
			projects.push(project);
			current = project;
		}
		
		public function remove(project:Project):void
		{
			// TODO: remove project function
			
			projects.slice(projects.indexOf(project), 1);
			project = null;
		}
		
		
		private function addToCookie(fileRef:FileReference):void
		{
			trace("adding cookie")
			var cookie:SharedObject = SharedObject.getLocal("ProjectManager");
			for (var i:uint = 0; i < 10; ++i) 
			{
				if (!cookie.data["file" + i])
				{
					cookie.data["file" + i] = fileRef;
					cookie.flush();
					trace("cookie added at", i);
					break;
				}
			}
			cookie.flush();
		}
		
		private function readCookies():void
		{
			trace("reading cookies")
			var cookie:SharedObject = SharedObject.getLocal("ProjectManager");
			for (var i:uint = 0; i < 10; ++i) 
			{
				if (cookie.data["file" + i] != null)
				{
					trace("cookie found at",i)
					trace("filename", cookie.data["file" + i].name);
				}
			}
		}
		
		
	}

}

class SingletonEnforcer { }
